package com.ouyeel.wlxer.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.adapter.ManualClockingAdapter;
import com.ouyeel.wlxer.base.BaseActivity;
import com.ouyeel.wlxer.bean.GoWrokIn;
import com.ouyeel.wlxer.bean.ManualOut;
import com.ouyeel.wlxer.bean.UploadqrcodeOut;
import com.ouyeel.wlxer.model.MainModel;
import com.ouyeel.wlxer.model.imp.MainModelImp;
import com.ouyeel.wlxer.util.GsonUtil;
import com.ouyeel.wlxer.util.SharePrefUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.RequestBody;

/**
 * Created by rankaifeng on 2017/10/30.
 */

public class ManualClockingActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.manual_list)
    ListView manualList;
    @BindView(R.id.rel_back)
    RelativeLayout relBack;
    @BindView(R.id.btn_go_work)
    Button btnGoWork;
    List<ManualOut.CarList> carListList = new ArrayList<>();
    private ManualClockingAdapter manualClockingAdapter;
    private String carNumber, token;
    private MainModel mainModel;

    @Override
    public int getLayout() {
        return R.layout.activity_manual_clocking;
    }

    @Override
    public void initView() {
        token = SharePrefUtil.getString(this, "token", "");
        relBack.setOnClickListener(this);
        btnGoWork.setOnClickListener(this);
        mainModel = new MainModelImp();
        String driverId = SharePrefUtil.getString(this, "driverId", "");
        Map<String, String> map = new HashMap<>();
        map.put("driverId", driverId);
        RequestBody requestBody = GsonUtil.getInstnse().changeBody(map);
        mainModel.getCarNumbers(this, requestBody);
        manualClockingAdapter = new ManualClockingAdapter(carListList, this);
        manualList.setAdapter(manualClockingAdapter);

        manualList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                manualClockingAdapter.selectPosition = position;
                manualClockingAdapter.notifyDataSetChanged();
                carNumber = carListList.get(position).getCarNum();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rel_back:
                finish();
                break;
            case R.id.btn_go_work:
                // TODO: 2017/10/30 调接口请求上班
                GoWrokIn goWrokIn = new GoWrokIn();
                goWrokIn.setCarNo(carNumber);
                goWrokIn.setToken(token);
//                Map<String, String> map = new HashMap<>();
//                map.put("carNo", carNumber);
//                map.put("token", token);
                mainModel.uploadqrcode(ManualClockingActivity.this, goWrokIn);
                break;
        }
    }

    /**
     * 扫描打卡上班
     *
     * @param out
     */
    public void goWorkSuccess(UploadqrcodeOut out) {
        SharePrefUtil.saveString(ManualClockingActivity.this, "clockstatus", "1");
        Intent mIntent = new Intent(ManualClockingActivity.this, MainActivity.class);
        startActivity(mIntent);
        showTost("上班打卡成功！");
        finish();
    }

    /**
     * 获取车牌号
     *
     * @param obj
     */
    public void getCarNumbersSuccess(ManualOut obj) {
        carListList.addAll(obj.getCarList());
        manualClockingAdapter.notifyDataSetChanged();
    }
}
